import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lighting_driver/src/moduls/home/model/model.dart';

import '../controllers/home_controller.dart';

class SelectView extends StatefulWidget {
  int row;
  Color? bgColor;
  SelectView({super.key, required this.row, this.bgColor});

  @override
  State<SelectView> createState() => _SelectViewState();
}

class _SelectViewState extends State<SelectView> {
  final HomeController controller = Get.find<HomeController>();

  @override
  void initState() {
    super.initState();
  }

  Widget _buildItem(double height, Model model, void Function() onTap) {
    Color color = widget.bgColor ?? Colors.transparent;
    return Obx(() {
      bool select = controller.selectModel.value == model;
      return Container(
        height: height,
        width: height,
        padding: const EdgeInsets.all(2),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(height / 2),
            border: Border.all(color: select ? color : Colors.transparent, width: 1.5),
            color: Colors.transparent),
        child: Center(
          child: Container(
            decoration: BoxDecoration(borderRadius: BorderRadius.circular((height - 2) / 2), color: color),
            clipBehavior: Clip.hardEdge,
            child: InkWell(
              splashColor: Colors.transparent,
              highlightColor: Colors.transparent,
              onTap: onTap,
              child: Center(
                child: model.isPic
                    ? Image.asset(model.name, width: height, height: height, fit: BoxFit.fill)
                    : Text('${model.name}%', style: const TextStyle(color: Colors.black)),
              ),
            ),
          ),
        ),
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
      var height = constraints.maxHeight - 10;
      List<Model> rows = controller.data![widget.row];
      return Container(
        height: height,
        padding: const EdgeInsets.only(top: 5, bottom: 5),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(16), border: Border.all(color: const Color(0xFF323232), width: 0.5)),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: List.generate(
              rows.length, ($0) => _buildItem(height, rows[$0], () => controller.changeValue(widget.row, $0))).toList(),
        ),
      );
    });
  }
}
